package com.example.wx.other;

import com.vivo.it.vwkc.pop.web.common.exception.BaseErrorInterface;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.EnumSet;

/**
 * @author 姜冲/72152416
 * @version 1.0
 * @date 2021/9/27 10:05
 * @description 业务错误
 */
public enum BizErrorEnum implements BaseErrorInterface {

    /**
     * 错误码200
     */
    SUCCESS("200", "成功"),
    /**
     * 错误码10000
     */
    ERROR("10000", "业务错误"),
    /**
     * 错误码10001
     */
    ACTIVE_PROCESS_AUDIT_ERROR("10001", "工作流审批失败"),

    ;


    /**
     * 错误码
     */
    private String resultCode;

    /**
     * 错误描述
     */
    private String resultMsg;

    BizErrorEnum(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    private MessageSource messageSource;

    public BizErrorEnum setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
        return this;
    }

    /**
     * 通过静态内部类的方式注入bean，并赋值到枚举中
     */
    @Component
    public static class ReportTypeServiceInjector {

        @Resource
        private MessageSource messageSource;

        @PostConstruct
        public void postConstruct() {
            for (BizErrorEnum rt : EnumSet.allOf(BizErrorEnum.class)) {
                rt.setMessageSource(messageSource);
            }
        }
    }

    @Override
    public String getResultCode() {
        return resultCode;
    }

    @Override
    public String getResultMsg() {
        return messageSource.getMessage(resultMsg,null,resultMsg, LocaleContextHolder.getLocale());
    }
}
